﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormLogin : Form
    {

        LoginHistoryManager historyManager = new LoginHistoryManager();
        AccountManager accountManager = new AccountManager();
        public Account currAccount;
        public IList<SystemOption> systemOptions;
        public FormLogin()
        {
            InitializeComponent();

            this.txtLoginId.Text = "operator";
            this.txtLoginPwd.Text = "pass";

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtLoginId.Text))
            {
                MessageBox.Show("Account is required!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtLoginId.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtLoginPwd.Text))
            {
                MessageBox.Show("Password is required", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtLoginPwd.Focus();
                return;
            }
            Account account = accountManager.GetAccountByAccountLoginId(txtLoginId.Text);
            if (null != account)
            {

                if (!txtLoginPwd.Text.Equals(account.LoginPwd))
                {

                    MessageBox.Show("Password incorrect", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;

                }
                else
                {

                    systemOptions = accountManager.GetAllOptions();

                    currAccount = account;
                    switch (account.Role)
                    {
                        case ConstData.ACCOUNT_ROLE_OPERATOR:
                        case ConstData.ACCOUNT_ROLE_ADMIN:
                            LoginHistory history = new LoginHistory();
                            history.LoginId = txtLoginId.Text.ToString();

                            history.LoginName = txtLoginId.Text;

                            history.StartTime = DateTime.Now.ToString();

                            historyManager.InsertHistory(history);


                            break;
                        case ConstData.ACCOUNT_ROLE_BOSS:
                            break;
                    }
                }
            }
            else
            {

                MessageBox.Show("Account not existing", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Dispose();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnInitialize_Click(object sender, EventArgs e)
        {
            accountManager.initialize();



        }
    }
}
